//
//  RowImagesCell.m
//  
//  jhf
//
//  Created by 计海峰 on 2020/8/3.
//  Copyright © 2020 jhfappkf@163.com. All rights reserved.
//

#import "RowImagesCell.h"
#import "RowImagesBaseM.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface RowImagesCell ()

@property (nonatomic, strong) NSMutableArray <UIImageView *> *imgsArray;

@end

@implementation RowImagesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)imageTap:(NSInteger)index {
    BindModel *bind = (BindModel *)self.model;
    RowImagesBaseM *control = bind.model;
    if (control.method) {
        if (index < control.imagesControls.count) {
            control.currentIdx = index;
            [bind.action run:control.method any:control];
        }else {
            NSLog(@"未知错误");
        }
    }else {
        NSLog(@"没有方法名");
    }
}

- (void)loadImges {
    BindModel *bind = (BindModel *)self.model;
    RowImagesBaseM *control = bind.model;
    NSArray <ImageControlM *> *array = control.imagesControls;
    [array enumerateObjectsUsingBlock:^(ImageControlM * _Nonnull config, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *img = self.imgsArray[idx];
        img.tag = 30000 + idx;
        [config controlIMG:img blockBool:nil];
    }];
}

- (void)bindTo:(__kindof id<BindProtocol>)model; {
    
    [super bindTo:model];
    BindModel *bind = (BindModel *)model;
    RowImagesBaseM *control = bind.model;
    NSArray <ImageControlM *> *array = control.imagesControls;
    
    if (array.count > self.imgsArray.count) {
        NSUInteger count = array.count - self.imgsArray.count;
        for (int i = 0; i < count; i++) {
            [self.imgsArray addObject:[self imgV]];
        }
    }
    
    [self.imgsArray enumerateObjectsUsingBlock:^(UIImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.hidden = idx >= array.count;
        if (!obj.hidden) {
            [obj setImage:[UIImage imageNamed:@"point_gray_f3f4f5"]];
        }
    }];
    
    // 是否延迟加载
    if (control.isDelay) {

    }else {
        [self loadImges];
    }
    
//    __block CGFloat leftCons = 0.f;
//    __block CGFloat topMaxCons = 0.f;
//    __block CGFloat heightMaxCons = 0.f; // 本行最高的按钮
//    __block CGFloat floorHeightCons = 0.f; //上次顶部高度
//    [array enumerateObjectsUsingBlock:^(ImageControlM *  _Nonnull config, NSUInteger idx, BOOL * _Nonnull stop) {
//        UIImageView *img = self.imgsArray[idx];
//        img.tag = 30000 + idx;
//        [config controlIMG:img blockBool:nil];
//
//        // 排放位置
//        // 左侧边距
//        leftCons += config.vLeftCons;
//        CGRect frame = CGRectMake(leftCons, config.vTopCons + floorHeightCons, config.vWidthCons, config.vHeightCons);
//        // 顶部距离 最大
//        topMaxCons = MAX(topMaxCons, config.vTopCons);
//        //最高的img
//        heightMaxCons = MAX(config.vHeightCons, heightMaxCons);
//        if ((leftCons + config.vWidthCons) > kScreen_Width && idx > 0) {
//            // 左侧边距+要添加按钮 大于背景宽度 且不是第一个 需要换行
//            leftCons = config.vLeftCons;
//            floorHeightCons = floorHeightCons + topMaxCons + heightMaxCons;
//            frame = CGRectMake(leftCons, floorHeightCons + config.vTopCons, config.vWidthCons, config.vHeightCons);
//            //设置新的 最高边距和最高按钮高度<新的一行 第一个>
//            topMaxCons = config.vTopCons;
//            heightMaxCons = config.vHeightCons;
//        }
//        // 设置frame后 增加 左侧宽度到 按钮的右侧
//        leftCons += config.vWidthCons;
//        // 更新按钮 frame
//        img.frame = frame;
//
//        if (idx == array.count - 1) {
//            /// 最后一个判断cell的高度刷新
//            CGFloat ideaCellHeight = floorHeightCons + (topMaxCons == 0 ? (config.vHeightCons + config.vTopCons) : (topMaxCons + heightMaxCons))  + config.vBottomCons;
//            if (bind.cellHeight < ideaCellHeight - 0.1) {
//                bind.cellHeight = ideaCellHeight;
//                [bind.action runService:RefreshRowHeight any:bind.action.key];
//            };
//        }
//    }];
}

- (NSMutableArray<UIImageView *> *)imgsArray {
    if (!_imgsArray) {
        _imgsArray = [NSMutableArray array];
        [_imgsArray addObject:[self imgV]];
    }
    return _imgsArray;
}

- (UIImageView *)imgV {
    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    imgV.userInteractionEnabled = true;
    @weakify(imgV);
    @weakify(self);
    [imgV addTapGestureRecognizer:^(UITapGestureRecognizer *recognizer, NSString *gestureId) {
        @strongify(imgV);
        @strongify(self);
        [self imageTap:imgV.tag - 30000];
    }];
    [self.bgV addSubview:imgV];
    return imgV;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
